/**
 *  我是学生-我的订单
 */
var setYear = function() {
	var currDate = new Date();
	var year     = currDate.getFullYear();
	$("#data_order_list").children("option[value=3]").html((year-1) + '年');
	$("#data_order_list").children("option[value=4]").html((year-2) + '年');
	$("#data_order_list").children("option[value=5]").html((year-3) + '年');
	$("#data_order_list").children("option[value=6]").html((year-3) + '年以前');
}

$(function(){
	$("#adv_srhBar").inputTips();
	$("#keyword_search_bar").inputTips();
	
	$("#keyword").keydown(function(event){
		if(event.keyCode == 13){
			$("#search_order").trigger('click');
		}
	});
	
	//切换高级、基础搜索
	$("#adv_sBr").click(function(){
	
		var cobj = $(this);
		var display = $("#adv_srhBar").css('display');
		if (display == 'none') {
			$("#adv_srhBar").slideDown('fast');
			cobj.removeClass("adv_sBr_down").addClass("adv_sBr_up").text('基础搜索');
		} else {
			$("#adv_srhBar").slideUp('fast');
			cobj.removeClass("adv_sBr_up").addClass("adv_sBr_down").text('高级搜索');
		}		
	});
	
	$("#search_order").click(function(){
		var orderid = $("#orderid").val();
		var sid = $("#sid").val();
		var start_time = $("#start_time").val();
		var end_time = $("#end_time").val();
		var nickname = $("#nickname").val();
		var keyword = $("#keyword").val();

		location.href = '/?mod=student&act=order&keyword='+encodeURIComponent(keyword)+'&orderid='+orderid+'&sid='+sid+'&start_time='+start_time+'&end_time='+end_time+'&nickname='+encodeURIComponent(nickname);
		return false;
	});
	
	//取消订单
	$(".canelOrder").click(function(){
		var oid = $(this).attr('oid');
		$.ckConfirm({
			message:"你确定要取消订单吗？",
			ok:function() {
				$.getJSON("/?mod=student&act=order&do=cancelOrder&oid="+oid+"&rand="+Math.random(), function(ret){
					if (ret.code == 0) {
						window.location.reload();
					} else {
						$.ckAlert({message:ret.data});
					}
				});
			}
		});
		return false;
	});
	
	//买家取消套餐订单
	$(".canelPackOrder").click(function(){
		var oid = $(this).attr('oid');
		$.ckConfirm({
			message:"你确定要取消订单吗？",
			ok:function() {
				$.getJSON("/?mod=student&act=order&do=cancelPackOrder&oid="+oid+"&rand="+Math.random(), function(ret){
					if (ret.code == 0) {
						window.location.reload();
					} else {
						$.ckAlert({message:ret.data});
					}
				});
			}
		});
		return false;
	});
	
	//确认打款
	$(".confirmPay").click(function(){
		var oid = $(this).attr('oid');
		var Box = $.ckAjaxBoxy({
			url : '/?mod=student&act=order&do=confirm&oid='+oid,
			title : "确认打款",
			width : 431,
			height : 316,
			callback:function(){
				$("#fe_dialogBox").find("a.cancel").unbind().bind('click', function(){
					Box.cancel();
				});
			}
		});
	});
	
	//付款前检查价格变更情况
	$(".payPackNow").click(function(){
		var oid = $(this).attr('oid');
		var url = $(this).attr('href');
		$.getJSON("/?mod=order&act=show&do=checkPackCost&oid="+oid+"&rand="+Math.random(), function(ret){
			if (ret.code == 0) {
				if (ret.data.Cost == '0.00') {
					$.ckAlert({message:'课程套餐价格异常'});
				} else {
					location.href = url;
				}		
			} else if(ret.code == 1) {
				//价格发生变更
				$.ckAlert({message:'该课程套餐的价格已经变更为'+ret.data.Cost+'元, 请取消订单重新下单'});
			}
		});
		return false;
	});
	
	//付款前检查价格变更情况
	$(".payNow2").click(function(){
		var oid = $(this).attr('oid');
		var url = $(this).attr('href');
		$.getJSON("/?mod=order&act=show&do=checkProductCost&oid="+oid+"&rand="+Math.random(), function(ret){
			if (ret.code == 1) {
				location.href = url;
			} else if (ret.code == 0) {
				$.ckTipsBoxy({
					type:"success", 
					width:352, 
					message:"购买成功，可以开始学习了。",
					mask:true,
					callback:function() {
						window.location.reload();
					}
				});
			} else {
				$.ckAlert({message:ret.data});
			}
		});
		return false;
	});
	
	setYear();
	
	$('#data_order_list').change(function() {
		var select = $(this).val();
		if (select != 0) {
			var strat_time = end_time = '';
			var currDate   = new Date();
			var year       = currDate.getFullYear();
			var month      = currDate.getMonth() + 1;
			var day        = currDate.getDate();
			switch (select) {
				case '1':
					strat_time = year + '-' + (currDate.getMonth() - 2) + '-' + day;
					end_time   = year + '-' + month + '-' + day;
					break;
				case '2':
					strat_time = year + '-1-1';
					end_time   = year + '-' + month + '-' + day;
					break;
				case '3':
					strat_time = (year - 1) + '-1-1';
					end_time   = (year - 1) + '-12-31';
					break;
				case '4':
					strat_time = (year - 2) + '-1-1';
					end_time   = (year - 2) + '-12-31';
					break;
				case '5':
					strat_time = (year - 3) + '-1-1';
					end_time   = (year - 3) + '-12-31';
					break;
				case '6':
					strat_time = '1990-1-1';
					end_time   = (year - 3) + '-1-1';
					break;
			}
			var url = 'http://www.chuanke.com/?mod=student&act=order&start_time=' + strat_time + '&end_time=' + end_time;
			location.href = url;
		}
	});
	
	$('td i.tc-other').click(function() {
		$(this).parent().parent().prevAll().show()
		$(this).parent().hide();
	});
});